<?php
function nvr_get_sidebar_position($postid = ''){
	$shortname = THE_SHORTNAME;
	$initial = THE_INITIAL;
	
	$pid = nvr_get_postid();
	$custom = nvr_get_customdata($pid);
	
	if($postid){
		$custom = nvr_get_customdata($postid);
	}
	
	$pagelayoutall = array('one-col','two-col-left','two-col-right');
	
	$sidebarposition = nvr_get_option( $shortname . '_sidebar_position' ,'two-col-left'); 
	$pagelayout = ($sidebarposition!="")? $sidebarposition : 'two-col-left';
	if(isset( $custom['_'.$initial.'_layout'][0] ) && $custom['_'.$initial.'_layout'][0]!='default'){
		$pagelayout = $custom['_'.$initial.'_layout'][0];
	}
	
	if(isset($_GET['sidebar_layout']) && in_array($_GET['sidebar_layout'],$pagelayoutall)){
		$pagelayout = esc_html($_GET['sidebar_layout']);
	}
	return $pagelayout;
}
function nvr_pf_get_image($imgsize, $postid=""){

	global $post;
	if($postid==""){
		$postid = get_the_ID();
	}

	$custom = get_post_custom( $postid );
	$cf_thumb = (isset($custom["custom_thumb"][0]))? $custom["custom_thumb"][0] : "";
	$cf_externallink = (isset($custom["external_link"][0]))? $custom["external_link"][0] : "";
	if(isset($custom["lightbox_img"])){
		$checklightbox = $custom["lightbox_img"] ; 
		$cf_lightbox = array();
		for($i=0;$i<count($checklightbox);$i++){
			if($checklightbox[$i]){
				$cf_lightbox[] = $checklightbox[$i];
			}
		}
		if(!count($cf_lightbox)){
			$cf_lightbox = "";
		}
	}else{
		$cf_lightbox = "";
	}
	
	
	/*get recent-portfolio-post-thumbnail*/
	$qrychildren = array(
		'post_parent' => $postid ,
		'post_status' => null,
		'post_type' => 'attachment',
		'order_by' => 'menu_order',
		'order' => 'ASC',
		'post_mime_type' => 'image'
	);

	$attachments = get_children( $qrychildren );
	
	$cf_thumb2 = array();
	$cf_full2 = "";
	$z = 1;
	foreach ( $attachments as $att_id => $attachment ) {
		$getimage = wp_get_attachment_image_src($att_id, $imgsize, true);
		$portfolioimage = $getimage[0];
		$alttext = get_post_meta( $attachment->ID , '_wp_attachment_image_alt', true);
		$image_title = $attachment->post_title;
		$caption = $attachment->post_excerpt;
		$description = $attachment->post_content;
		$cf_thumb2[] ='<img src="'.$portfolioimage.'" alt="'.$alttext.'" title="'. $image_title .'" class="scale-with-grid" />';
		
		$getfullimage = wp_get_attachment_image_src($att_id, 'full', true);
		$fullimage = $getfullimage[0];
		
		if($z==1){
			$fullimageurl = $fullimage;
			$fullimagetitle = $image_title;
			$fullimagealt = $alttext;
		}elseif($att_id == get_post_thumbnail_id( $postid ) ){
			$cf_full2 ='<a data-rel="prettyPhoto['.$post->post_name.']" href="'.$fullimageurl.'" title="'. $fullimagetitle .'" class="hidden"></a>'.$cf_full2;
			$fullimageurl = $fullimage;
			$fullimagetitle = $image_title;
			$fullimagealt = $alttext;
		}else{
			$cf_full2 .='<a data-rel="prettyPhoto['.$post->post_name.']" href="'.$fullimage.'" title="'. $image_title .'" class="hidden"></a>';
		}
		$z++;
	}
	
	if($cf_thumb!=""){
		$cf_thumb = '<img src="' . $cf_thumb . '" alt="'. get_the_title($postid) .'"  class="scale-with-grid" />';
	}elseif( has_post_thumbnail( $postid ) ){
		$cf_thumb = get_the_post_thumbnail($postid, $imgsize, array('class' => 'scale-with-grid'));
	}elseif( isset( $cf_thumb2[0] ) ){
		$cf_thumb = $cf_thumb2[0];
	}else{
		$cf_thumb = '<span class="nvr-noimage"></span>';
	}
	
	
	if($cf_externallink!=""){
		$golink = $cf_externallink;
		$rollover = "gotolink";
		$cf_full2 = '';
	}else{
		$golink = get_permalink();
		$rollover = "gotopost";
	}
	
	$bigimageurl = '';
	if( is_array($cf_lightbox) ){
		$bigimageurl = $cf_lightbox[0];
		$bigimagetitle = get_the_title();
		$rel = ' data-rel="prettyPhoto['.$post->post_name.']"';
		$cf_lightboxoutput = '';
		for($i=1;$i<count($cf_lightbox);$i++){
			$cf_lightboxoutput .='<a data-rel="prettyPhoto['.$post->post_name.']" href="'.$cf_lightbox[$i].'" title="'. get_the_title($postid) .'" class="hidden"></a>';
		}
		$cf_full2 = $cf_lightboxoutput;
	}else{
		if( isset($fullimageurl)){
			$bigimageurl = $fullimageurl; 
			$bigimagetitle = $fullimagetitle;
			$rel = ' data-rel="prettyPhoto['.$post->post_name.']"';
		}
	}
	
	$return = array(
		'bigimageurl' 	=> $bigimageurl,
		'bigimagetitle'	=> $bigimagetitle,
		'rel'			=> $rel,
		'cf_full2'		=> $cf_full2,
		'golink'		=> $golink,
		'rollover'		=> $rollover,
		'cf_thumb'		=> $cf_thumb
	);
	return $return;
}

function nvr_pf_get_box( $imgsize, $postid="",$class="", $limitchar = 250 ){

	$output = "";
	global $post;
	
	if($postid==""){
		$postid = get_the_ID();
	}
	$taxonomy_slug = 'portfoliocat';
	
	$get_image = nvr_pf_get_image($imgsize, $postid );
	extract($get_image);
	
	$output  .='<li class="'.$class.'">';
		$output  .='<div class="nvr-pf-box">';
			$output  .='<div class="nvr-pf-img">';
				$output .='<div class="rollover"></div>';
				
				$output .='<a class="image '.$rollover.'" href="'.$golink.'" title="'.get_the_title($postid).'"></a>';
				if($bigimageurl!=''){
					$output .='<a class="image zoom" href="'.$bigimageurl.'" '.$rel.' title="'.$bigimagetitle.'"></a>';
				}
				
				$output  .=$cf_thumb;
				$output  .=$cf_full2;
			$output  .='</div>';
	
			$excerpt = nvr_string_limit_char( get_the_excerpt(), $limitchar );
			$output  .='<div class="nvr-pf-text">';
			
				$output  .='<h2 class="nvr-pf-title"><a href="'.get_permalink($postid).'" title="'.get_the_title($postid).'">'.get_the_title($postid).'</a></h2>';
				 // get the terms related to post
				$terms = get_the_terms( $postid, $taxonomy_slug );
				$termarr = array();
				if ( !empty( $terms ) ) {
				  foreach ( $terms as $term ) {
					$termarr[] = '<a href="'. get_term_link( $term->slug, $taxonomy_slug ) .'">'. $term->name ."</a>";
				  }
				  
				  $output .= '<div class="nvr-pf-cat">'.implode(", ", $termarr).'</div>';
				}
				$output .= '<div class="nvr-pf-separator"></div>';
				$output .= '<div class="nvr-pf-content">'.$excerpt.'</div>';
				
			$output  .='</div>';
			$output  .='<div class="nvr-pf-clear"></div>';
		$output  .='</div>';
	$output  .='</li>';
	
	return $output; 
}

if( !function_exists('nvr_section_builder') ){
	function nvr_section_builder($sectionbuilders){

		if(isset($sectionbuilders) && is_array($sectionbuilders) && count($sectionbuilders)>0){ 
			$i = 1;
			foreach($sectionbuilders as $sectionbuilder){ 
				
				$sectionbgcolor = (isset($sectionbuilder['backgroundcolor']))? $sectionbuilder['backgroundcolor'] : '';
				$sectionbgimage = (isset($sectionbuilder['backgroundimage']))? $sectionbuilder['backgroundimage'] : '';
				$sectionclass   = (isset($sectionbuilder['customclass']))? $sectionbuilder['customclass'] : '';
				$sectioncontent = (isset($sectionbuilder['content']))? $sectionbuilder['content'] : '';
				
				if($sectioncontent==''){ continue; }
				
				$sectionstyle = '';
				if($sectionbgcolor!='' || $sectionbgimage!=''){
					$sectionstyle  = 'style="';
						if($sectionbgcolor!=''){
							$sectionstyle .= 'background-color:'.$sectionbgcolor.'; ';
						}
						if($sectionbgimage!=''){
							$sectionstyle .= 'background-image:url('.$sectionbgimage.'); ';
						}
						$sectionstyle .= 'background-repeat:no-repeat';
						$sectionstyle .= 'background-position:center';
					$sectionstyle .= '"';
				}
			?>
				<section id="outersection_<?php echo $i; ?>" class="outersection <?php echo $sectionclass; ?>" <?php echo $sectionstyle; ?>>
					<div class="container">
						<section id="innersection_<?php echo $i; ?>" class="row innersection">
							<div class="sectioncontent twelve columns">
								<?php echo do_shortcode($sectioncontent); ?>
								<div class="clearfix"></div>
							</div>
							<div class="clearfix"></div>
						</section>
					</div>
				</section>
			<?php 
				$i++;
			}//end foreach 
		}
	}
}

/*Template for comments and pingbacks. */
if ( ! function_exists( 'nvr_comment' ) ) :
function nvr_comment( $comment, $args, $depth ) {
	$GLOBALS['comment'] = $comment;
	switch ( $comment->comment_type ) :
		case '' :
	?>
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
		<div id="comment-<?php comment_ID(); ?>" class="con-comment">
		<div class="comment-author vcard">
			<?php echo get_avatar( $comment, 89 ); ?>
		</div><!-- .comment-author .vcard -->


		<div class="comment-body">
			<?php  printf( __( '<cite class="fn">%s</cite>', THE_LANG ), sprintf( '%s Says:', get_comment_author_link() ) ); ?>
            <span class="time">
            <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
            <?php
                /* translators: 1: date, 2: time */
                printf( __( '%1$s %2$s', THE_LANG ), get_comment_date(),  get_comment_time() ); ?></a>
                <?php edit_comment_link( __( '(Edit)', THE_LANG ), ' ' );?>
            </span>
            
            <div class="clear"></div>
			<div class="commenttext">
			<?php comment_text(); ?>
			<?php if ( $comment->comment_approved == '0' ) : ?>
				<em><?php _e( 'Your comment is awaiting moderation.', THE_LANG ); ?></em>
			<?php endif; ?>
			</div>
            <span class="reply"><?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ,'reply_text' => __('Reply this comment', THE_LANG)) ) ); ?></span>
			<div class="clear"></div>
		</div>
		<div class="clear"></div>
	</div><!-- #comment-##  -->

	<?php
			break;
		case 'pingback'  :
		case 'trackback' :
			echo '<li class="post pingback">';
				echo '<p>'. __( 'Pingback:', THE_LANG ).' ';
					comment_author_link();
					edit_comment_link( __('(Edit)', THE_LANG), ' ' );
				echo '</p>';
				
			break;
	endswitch;
}
endif;

if ( ! function_exists( 'nvr_share_button_output' ) ) :

function nvr_share_button_output() {
	if(function_exists('ssba_buttons')){
	echo '<div class="sharebutton-container">';
		echo '<h6 class="sharebutton-title"><span class="sbt">'.__('Share On', THE_LANG).'</span></h6>';
		echo do_shortcode('[ssba]');
	echo '</div>';
	}
}
add_action('nvr_share_button','nvr_share_button_output',1);
endif;

/*********QUICK READ POST**********/
add_action("wp_ajax_nvr_quickread", "nvr_quickread");
add_action("wp_ajax_nopriv_nvr_quickread", "nvr_quickread");
function nvr_quickread(){
	
	if( !wp_verify_nonce( $_REQUEST['nonce'], "nvr_quickread_nonce")) {
    	exit("No naughty business please");
	}
	$initial = THE_INITIAL;
	$postid = (isset($_REQUEST["post_id"]) && $_REQUEST["post_id"]>0)? $_REQUEST["post_id"] : 0;
	
	$query_args = array('p'	=> $postid);
	
	$outputjson = array();
	
	$r = new WP_Query($query_args);
	global $post, $more;
 	$more = 1;
	if($r->have_posts()){ 

		while ($r->have_posts()){ $r->the_post(); setup_postdata($r->post);
			
			$outputjson['test'] = get_the_ID();
			$outputjson['postid'] = get_the_ID();
			$outputjson['slug'] = $post->post_name;
			$outputjson['title'] = get_the_title();
			$outputjson['content'] = do_shortcode( get_the_content() );
			$outputjson['uploadedimg'] = $outputjson['featuredimg'] = array();
			
			/*get recent-portfolio-post-thumbnail*/
			
			$thumbid = has_post_thumbnail($postid)? get_post_thumbnail_id($postid) : 0;
			$mediacontainer = '';
			
			$custom = get_post_custom($postid);
			$_post_format = get_post_format();
			switch ($_post_format) {
				case "video":
					
					$cf_vidurl = (isset($custom["_".$initial."_video_url"][0]))? $custom["_".$initial."_video_url"][0] : "";
					if($cf_vidurl!=''){
						$mediacontainer = '<div class="mediacontainer">'.apply_filters('the_content', $cf_vidurl)."</div>";
					}
					$iconclass = "fa-film";
					
				break;
				case "audio":
				
					$cf_audurl = (isset($custom["_".$initial."_audio_url"][0]))? $custom["_".$initial."_audio_url"][0] : "";
					if($cf_audurl!=''){
						$mediacontainer = '<div class="mediacontainer">'.apply_filters('the_content', $cf_audurl)."</div>";
					}
					$iconclass = "fa-volume-up";
				
				break;
				case "gallery":
					
					$post_content = get_the_content();
					preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
					$array_id = explode(",", $ids[1]);
					
					$content =  str_replace($ids[0], "", $post_content);
					$filtered_content = apply_filters( 'the_content', $content);
					
					$sliderli = '';
					foreach($array_id as $img_id){
						$sliderli .= '<li><a href="'. get_permalink() .'">'. wp_get_attachment_image( $img_id, 'blog-post-image' ) .'</a></li>';
					}
					
					if($sliderli!=''){
						$mediacontainer = '<div class="gallerycontainer"><div class="flexslider"><ul class="slides">'.$sliderli."</ul></div></div>";
					}
					$iconclass = "fa-image";
					
				break;
				case "image":
					
					$cf_imgurl = (isset($custom["image_url"][0]))? $custom["image_url"][0] : "";
					$imgurl = "";
					/* temporary not used */
					if($cf_imgurl!=""){
						$imgurl = '<img src='. $cf_imgurl .' alt="'. get_the_title( $post->ID ).'" class="scale-with-grid"/>';
					}elseif(has_post_thumbnail($post->ID) ){
						$imgurl = get_the_post_thumbnail($post->ID, 'blog-post-image', array('class' => 'scale-with-grid'));
					}else{
						$imgurl ="";
					}
					
					if($imgurl!=''){
						$mediacontainer = '<div class="imgcontainer">'.$imgurl."</div>";
					}
					$iconclass = "fa-camera";
					
				break;
				
				default :
					$iconclass = "fa-file-text-o";
				
				break;
			}
			
			if(isset($filtered_content)){
				$outputjson['content'] = do_shortcode( $filtered_content );
			}else{
				$outputjson['content'] = do_shortcode( get_the_content() );
			}
			
			$postclass = get_post_class('', $postid);
			
			$output .= '<div id="singlepost">';
				$output .= '<article id="post-'.$outputjson['postid'].'" class="'.implode(", ", $postclass).'">';
					$output .= $mediacontainer;
					$output .= '<h2 class="posttitle">'.$outputjson['title'].'</h2>';
					$output .= '<div class="entry-utility">';
						$output .= '<span class="meta-author">'. __('By', THE_LANG).' <a href="'. get_author_posts_url( get_the_author_meta( 'ID' ) ) .'">'. get_the_author() .'</a></span> &nbsp;&nbsp;/&nbsp;&nbsp;';
						$output .= '<span class="meta-date"><i class="fa fa-calendar"></i>&nbsp; '. get_the_time('M d, Y', $postid) .'</span>&nbsp;&nbsp;/&nbsp;&nbsp;';
						$categories = get_the_category( $postid );
						$catarr = array();
						if ( !empty( $categories ) ) {
						  foreach ( $categories as $category ) {
							$catarr[] = $category->name;
						  }
						  $output .= '<span class="meta-cat"><i class="fa fa-folder-open"></i>&nbsp; '.implode(", ", $catarr).'</span>';
						}
					$output .= '</div>';
					$output .= '<div class="entry-icon"><div class="fa '.$iconclass.'"></div></div>';
					$output .= '<div id="content-'.$outputjson['postid'].'" class="entry-content">';
						$output .= $outputjson['content'];
					$output .= '</div>';
					$output .= '<div class="clearfix margin30"></div>';
				$output .= '</article>';
			$output .= '</div>';
			$output .= '<p></p>';
		}
		
	}
	echo $output;
	//wp_send_json($outputjson);
	die();
}